package cn.cuiliangpeng.org.service.impl;

import cn.cuiliangpeng.basic.consts.RedisConstants;
import cn.cuiliangpeng.basic.exception.BusinessException;
import cn.cuiliangpeng.basic.exception.ErrorCode;
import cn.cuiliangpeng.basic.utils.BaiduAuditUtils;
import cn.cuiliangpeng.org.domain.Employee;
import cn.cuiliangpeng.org.domain.Shop;
import cn.cuiliangpeng.org.domain.ShopAuditLog;
import cn.cuiliangpeng.org.domain.vo.ShopEcharts;
import cn.cuiliangpeng.org.mapper.EmployeeMapper;
import cn.cuiliangpeng.org.mapper.ShopAuditLogMapper;
import cn.cuiliangpeng.org.mapper.ShopMapper;
import cn.cuiliangpeng.org.service.IShopService;
import cn.cuiliangpeng.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //1.校验  使用了注解校验了
        //2.校验二次密码是否正确
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BusinessException(ErrorCode.PASS_WORD.getCode());
        }
        Boolean auditFlag = false;
        //百度AI文本智能审核
        auditFlag=BaiduAuditUtils.TextCensor(shop.getName());
        //百度AI图片智能审核
        if (shop.getLogo()!=null){
            auditFlag=BaiduAuditUtils.ImgCensor(RedisConstants.HTTP_KEY +shop.getLogo());
        }
        if (!auditFlag){
            throw new BusinessException(ErrorCode.CONTENT_AUDIT_FAILED.getCode());
        }
        //3.校验店铺是否注册过 用名字验证
        Shop shopName=shopMapper.findByName(shop.getName());
        if (shopName!=null){
            throw new BusinessException("店铺已经存在，请登录查看");
        }
        //4.通过校验保存店铺
        shopMapper.save(shop);
        //5.校验判断店长是否注册
        Employee emp=employeeMapper.findByUserName(shop.getAdmin().getUsername());
        //6.校验店长和店铺是否存在  存在表示不可用
        if (emp!=null && emp.getShop_id()!=null){  //表示存在
            throw new BusinessException("店长已经存在");
        }
        //7.如果店长存在但是没有shopid就直接绑定本次shop的id
        if (emp!=null){
            emp.setShop_id(shop.getId());
            shop.setAdminId(emp.getId());
        }else {
            emp=shop.getAdmin();
            emp.setShop_id(shop.getId()); //员工绑定店铺id
            employeeMapper.save(emp); //添加员工
            shop.setAdminId(shop.getAdmin().getId());//给店铺绑定店长
        }
        shopMapper.update(shop); //更新店铺
    }

    /**
     * 审核通过  待激活状态
     * @param log
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.修改店铺和(店长的状态应该再激活店铺的时候改为正常状态)
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(2);//修改店铺为待激活状态
        shopMapper.update(shop);
        //2.添加日志  1通过0驳回
        log.setState(1);
        log.setShopId(325L);  //设置审核人（以后做成动态的）
        shopAuditLogMapper.save(log);

        //3.发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("431045028@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()+"' ><a href='http://127.0.0.1:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        Employee empLoyee = employeeMapper.findById(shop.getAdminId());
        //收件人
        helper.setTo(empLoyee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    /**
     * 驳回
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.修改店铺和(店长的状态应该再激活店铺的时候改为正常状态)
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(-1);//修改店铺为待激活状态
        shopMapper.update(shop);
        //2.添加日志  1通过0驳回
        log.setState(0);
        log.setShopId(325L);  //设置审核人（以后做成动态的）
        shopAuditLogMapper.save(log);
        //3.发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("431045028@qq.com");
        helper.setSubject("店铺审核失败");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺未通过，请重新填写资料在入驻!!!</h1>");
        Employee empLoyee = employeeMapper.findById(shop.getAdminId());
        //收件人
        helper.setTo(empLoyee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    /**
     * echarts 图形显示
     * @return
     */
    @Override
    public List<ShopEcharts> echarts() {
        return shopMapper.echarts();
    }

    /**
     * 店铺邮件激活业务
     * @param id
     */
    @Override
    public void active(Long id) {
        Shop shop = shopMapper.findById(id);
        shop.setState(1);
        shopMapper.update(shop);
    }
}
